package geektime.spring.springbucks;

import com.github.pagehelper.PageHelper;
import geektime.spring.springbucks.dao.CoffeeDao;
import geektime.spring.springbucks.model.Coffee;
import geektime.spring.springbucks.model.CoffeeOrder;
import geektime.spring.springbucks.model.OrderState;
// import geektime.spring.springbucks.service.CoffeeOrderService;
import geektime.spring.springbucks.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;

import org.joda.money.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Optional;
import java.util.Properties;

@Slf4j
@EnableTransactionManagement
@SpringBootApplication
public class SpringBucksApplication {

	@Autowired
	private CoffeeService coffeeService;
	
	public static void main(String[] args) {
		SpringApplication.run(SpringBucksApplication.class, args);
	}

	/*如果SpringBucksApplication实现了ApplicationRunner接口，那么可以重写以下方法
	* implements ApplicationRunner
	* */
//	@Override
//	public void run(ApplicationArguments args) throws Exception {
//		log.info("All Coffee: {}", coffeeService.allCoffees());
//		Coffee coffee = new Coffee();
//		coffee.setName("espresso");
//		coffee.setPrice(20000L);
//		log.info("All Coffee: {}", coffeeService.updateCoffeeByName(coffee));
//		coffee.setPrice(30000L);
//		try{
//			log.info("Exception: {}", coffeeService.updateCoffeeByNameTextException(coffee));
//		}catch(Exception e){
//			log.info("Exception: {}", coffeeService.findCoffeeByName(coffee));
//
//		}
//		log.info("aaa: {}", coffeeService.findCoffeeByName(coffee));
//		log.info("分页1: {}", coffeeService.findAllWithParam("1","4"));
//		log.info("分页2: {}", coffeeService.findAllWithParam("2","2"));

//	}

	//配置mybatis的分页插件pageHelper
	@Bean
	public PageHelper pageHelper() {
		PageHelper pageHelper = new PageHelper();
		Properties props = new Properties();
		props.setProperty("dialect", "mysql");
		// 表示支持从接口中读取pageNum和pageSize
		props.setProperty("supportMethodsArguments", "true");
		pageHelper.setProperties(props);
		return pageHelper;
	}

}

